from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options


class ChromeDriver:
    def __init__(self, executable_path='../chromedriver.exe', headless=False):
        self.executable_path = executable_path
        self.headless = headless
        self.driver = self._initialize_driver()

    def _initialize_driver(self):
        chrome_options = Options()
        if self.headless:
            chrome_options.add_argument("--headless")  # 无头模式
        chrome_options.add_argument("--no-sandbox")
        chrome_options.add_argument("--disable-dev-shm-usage")

        service = Service(self.executable_path)
        driver = webdriver.Chrome(service=service, options=chrome_options)
        return driver

    def get(self, url):
        self.driver.get(url)

    def find_element(self, by, value):
        return self.driver.find_element(by, value)

    def close(self):
        self.driver.quit()

    def maximize_window(self):
        self.driver.maximize_window()

    @property
    def title(self):
        return self.driver.title
